﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace GreetingCreatorSample
{
    [TemplateVisualState(Name = "ReadMode", GroupName = "PresentationStates")]
    [TemplateVisualState(Name = "EditMode", GroupName = "PresentationStates")]
    public partial class Page : UserControl
    {
        private Random random = new Random();

        public Page()
        {
            InitializeComponent();            
        }

        private bool? _isReadOnly = null;
        public bool? IsReadOnly
        {
            get { return _isReadOnly; }
            set 
            {                 
                _isReadOnly = value;                

                if (_isReadOnly == true)
                {
                    VisualStateManager.GoToState(this, "ReadMode", false);                    
                }
                else
                {
                    VisualStateManager.GoToState(this, "EditMode", false);
                }
                
                greetingCreator.IsReadOnly = _isReadOnly.GetValueOrDefault();
            }
        }

        private void OnImageSelected(object sender, ItemPicker.StringEventArgs e)
        {
            GreetingCreator.GreetingServiceReference.GraphicElementData addedElement = greetingCreator.AddGraphicElement(e.Value, "ImageTemplate");            
        }

        private void OnWordSelected(object sender, ItemPicker.StringEventArgs e)
        {            
            GreetingCreator.GreetingServiceReference.GraphicElementData addedElement = greetingCreator.AddGraphicElement(e.Value, "WordTemplate");
            addedElement.Angle = random.NextDouble() * 90.0 - 45.0;
            addedElement.PositionX = random.Next(-50, 50);
            addedElement.PositionY = random.Next(-50, 50);
        }

        private void OnItemCloseRequested(object sender, EventArgs e)
        {
            ContentPresenter cp = ((TransformInteraction.TransformControl)sender).Content as ContentPresenter;
            greetingCreator.RemoveGraphicElement(cp.Content);
        }        
    }
}
